package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnCustomerBo;
import com.ruoyi.system.domain.vo.KfnCustomerVo;
import com.ruoyi.system.domain.KfnCustomer;
import com.ruoyi.system.mapper.KfnCustomerMapper;
import com.ruoyi.system.service.IKfnCustomerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * KFN客户管理Service业务层处理
 *
 * @author Panda
 * @date 2023-12-20
 */
@RequiredArgsConstructor
@Service
public class KfnCustomerServiceImpl implements IKfnCustomerService {

    private final KfnCustomerMapper baseMapper;

    /**
     * 查询KFN客户管理
     */
    @Override
    public KfnCustomerVo queryById(Long customerId){
        return baseMapper.selectVoById(customerId);
    }

    /**
     * 查询KFN客户管理列表
     */
    @Override
    public TableDataInfo<KfnCustomerVo> queryPageList(KfnCustomerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnCustomer> lqw = buildQueryWrapper(bo);
        Page<KfnCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询KFN客户管理列表
     */
    @Override
    public List<KfnCustomerVo> queryList(KfnCustomerBo bo) {
        LambdaQueryWrapper<KfnCustomer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnCustomer> buildQueryWrapper(KfnCustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnCustomer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), KfnCustomer::getCustomerName, bo.getCustomerName());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerContacts()), KfnCustomer::getCustomerContacts, bo.getCustomerContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerContactNumber()), KfnCustomer::getCustomerContactNumber, bo.getCustomerContactNumber());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerReferences()), KfnCustomer::getCustomerReferences, bo.getCustomerReferences());
        return lqw;
    }

    /**
     * 新增KFN客户管理
     */
    @Override
    public Boolean insertByBo(KfnCustomerBo bo) {
        KfnCustomer add = BeanUtil.toBean(bo, KfnCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerId(add.getCustomerId());
        }
        return flag;
    }

    /**
     * 修改KFN客户管理
     */
    @Override
    public Boolean updateByBo(KfnCustomerBo bo) {
        KfnCustomer update = BeanUtil.toBean(bo, KfnCustomer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnCustomer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除KFN客户管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
